var multer = require('multer');
var config = require("./config");
var image_storage = multer.diskStorage({//multer存储引擎  存储引擎自定义引用 https://github.com/expressjs/multer/blob/master/StorageEngine.md
    destination: function (req, file, cb){
        cb(null, './dist' + config.imagePathFormat)
    },//指定上传文件的路径
    filename: function (req, file, cb) { 
        cb(null, Date.now() + '-' + file.originalname);
    } 
});
var image_multer = multer({ 
    storage: image_storage,
}).single('upfile');//single 单文件上传，upfile为form表单中 接受文件的name字段名称
var video_storage = multer.diskStorage({//指定上传文件的路径
    destination: function (req, file, cb){
        cb(null, './dist' + config.videoPathFormat);
    },
    filename: function (req, file, cb) { 
        cb(null, Date.now() + '-' + file.originalname);
    } 
})
var video_multer = multer({ 
    storage: video_storage,
}).single('upfile');

module.exports = {
    upload_file: function(req,res){
        console.log("编辑器上传的,");
        switch(req.query.action){
            case 'uploadimage':{
                //这个里面才能获取到file
                image_multer(req,res,function(err){
                    if(err){
            　　　　　　console.log(err);
            　　　　　　return;
            　　    }
                    res.json({
                        "state": "SUCCESS",
                        "url": req.file.path.replace(/^dist\\/, '').replace(/\\/g, '/'),
                        "title": req.file.filename,
                        "original": req.file.filename
                    });
            　　});
            };break;
            case 'uploadvideo':{
                video_multer(req, res, function(err){
                    if(err){
            　　　　　　console.log(err);
            　　　　　　return;
            　　    }
                    res.json({
                        "state": "SUCCESS",
                        "url": req.file.path.replace(/^dist\\/, '').replace(/\\/g, '/'),
                        "title": req.file.filename,
                        "original": req.file.filename
                    });
                })
            };break
        }
    },
};